package vip.xiaonuo.biz.modular.event.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import vip.xiaonuo.biz.modular.apilog.entity.DevApiLog;
import vip.xiaonuo.biz.modular.apilog.mapper.DevApiLogMapper;
import vip.xiaonuo.biz.modular.cabinetinfo.entity.BizCabinetInfo;
import vip.xiaonuo.biz.modular.event.entity.BizEvent;
import vip.xiaonuo.biz.modular.event.entity.BizEventConfig;
import vip.xiaonuo.biz.modular.event.mapper.BizEventConfigMapper;
import vip.xiaonuo.biz.modular.event.mapper.BizEventMapper;
import vip.xiaonuo.biz.modular.event.service.BizEventReplaceService;
import vip.xiaonuo.biz.modular.event.service.BizEventWebHookService;
import vip.xiaonuo.biz.modular.event.util.EventActionUtil;
import vip.xiaonuo.common.util.AliyunSmsUtil;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @author 墨塵
 * @since 2023/3/11
 */

@Service
@Transactional
public class BizEventWebHookServiceImpl implements BizEventWebHookService {

    @Resource
    private BizEventConfigMapper bizEventConfigMapper;

    @Resource
    private BizEventReplaceService bizEventReplaceService;

    @Resource
    private BizEventMapper bizEventMapper;

    @Resource
    private DevApiLogMapper devApiLogMapper;


    @Override
    public void handleAction(DevApiLog apiLog) {
        //解析data
        JSONObject dataJson = JSONObject.parseObject(apiLog.getReqJson()).getJSONObject("data");
        if (null != dataJson) {
            String msg = dataJson.getString("msg");
            String value = dataJson.getString("value");
            String eventType = dataJson.getString("eventType");
            if (eventType.equals("预警事件")) {
                this.addRecordForEarlyWaring(msg, value, apiLog);
            }
            if (eventType.equals("报警事件")) {
                this.addRecordForAlarm(msg, value, apiLog);
            }
            if (eventType.equals("操作事件")) {
                this.updateEventId(value, dataJson);
            }
        }
    }

    /**
     * 添加记录-环境预警事件
     *
     * @param goodsList
     * @param apiLog
     */
    private void addRecordForEarlyWaring(String msg, String value, DevApiLog apiLog) {
        //查询对应配置
        BizEventConfig eventConfig = bizEventConfigMapper.selectById("32");
        //事件描述
        String eventDesc = eventConfig.getEventDesc();
        //柜子名称
        BizCabinetInfo cabInfo = bizEventReplaceService.getCabInfo(apiLog.getCabinetCode());
        String cabName = bizEventReplaceService.getCabName(apiLog.getCabinetCode());
        eventDesc = eventDesc.replace("{{cab}}", cabName);
        eventDesc = eventDesc.replace("{{msg}}", msg + "异常[" + value + "]");
        //组合实体类
        BizEvent event = BeanUtil.toBean(eventConfig, BizEvent.class);
        event.setApiLogId(apiLog.getId());
        event.setEventConfigId(eventConfig.getId());
        event.setEventTime(apiLog.getEventTime());
        event.setEventDesc(eventDesc);
        event.setEventAction(EventActionUtil.getEventAction(apiLog.getReqType()));
        event.setCabinetGroupId(cabInfo.getCabinetGroupId());
        event.setCabinetId(cabInfo.getId());
        bizEventReplaceService.clearBizEvent(event);
        bizEventMapper.insert(event);

        //【邮件】本次柜子管理员
        bizEventReplaceService.sendEmailForCabAdmin(event);
    }

    /**
     * 添加记录-环境报警事件
     *
     * @param goodsList
     * @param apiLog
     */
    private void addRecordForAlarm(String msg, String value, DevApiLog apiLog) {
        //查询对应配置
        BizEventConfig eventConfig = bizEventConfigMapper.selectById("31");
        //事件描述
        String eventDesc = eventConfig.getEventDesc();
        //柜子名称
        BizCabinetInfo cabInfo = bizEventReplaceService.getCabInfo(apiLog.getCabinetCode());
        String cabName = bizEventReplaceService.getCabName(apiLog.getCabinetCode());
        eventDesc = eventDesc.replace("{{cab}}", cabName);
        eventDesc = eventDesc.replace("{{msg}}", msg + "异常[" + value + "]");
        //组合实体类
        BizEvent event = BeanUtil.toBean(eventConfig, BizEvent.class);
        event.setApiLogId(apiLog.getId());
        event.setEventConfigId(eventConfig.getId());
        event.setEventTime(apiLog.getEventTime());
        event.setEventDesc(eventDesc);
        event.setEventAction(EventActionUtil.getEventAction(apiLog.getReqType()));
        event.setCabinetGroupId(cabInfo.getCabinetGroupId());
        event.setCabinetId(cabInfo.getId());
        bizEventReplaceService.clearBizEvent(event);
        bizEventMapper.insert(event);

        //【邮件+短信】本次柜子管理员
        bizEventReplaceService.sendEmailForCabAdmin(event);
        Map<String, String> content = new HashMap<>();
        content.put("cabinetName", cabName);
        content.put("msg", msg);
        content.put("value", value);
        bizEventReplaceService.sendSmsForCabAdmin(event, AliyunSmsUtil.ABNORMAL_ENVIRONMENT, content);
    }

    private void updateEventId(String value, JSONObject dataJson) {
        if (StringUtils.isNotBlank(value)) {
            String apiLogId = value.substring(value.indexOf("-") + 1, value.length());
            String eventId = dataJson.getString("eventid");
            if (StringUtils.isNotBlank(apiLogId) && StringUtils.isNotBlank(eventId)) {
                //更新apilog表
                devApiLogMapper.updateEventId(eventId, apiLogId);
                //更新event表
                bizEventMapper.updateEventId(eventId, apiLogId);
            }
        }
    }
}
